package com.dhh.util.manager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dhh.dao.DataBaseHelper;
import com.dhh.dao.FoodLogDAO;
import com.dhh.vo.DailyEnergy;
import com.dhh.vo.FoodLog;
import com.dhh.vo.PlanItem;

public class FoodLogManager {	
	private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
	private FoodLogDAO dao;
	
	public FoodLogManager(DataBaseHelper db){
		dao = new FoodLogDAO(db);
	}
	
	public String getTodayEnergy(){
		String today = sf.format(Calendar.getInstance().getTime());
		List<FoodLog> logs = dao.getFoodLog(today);
		double sum = 0;
		for(FoodLog log : logs){
			double e = Double.parseDouble(log.getEnergy());
			sum += e;
		}
		return String.valueOf(Math.round(sum));
	}
	
	public String getYesterdayEnergy(){
		Calendar c = Calendar.getInstance();
		Date date = c.getTime();
		date.setTime(c.getTime().getTime()- 1000*60*60*24);
		String yesterday = sf.format(date);
		List<FoodLog> logs = dao.getFoodLog(yesterday);
		double sum = 0;
		for(FoodLog log : logs){
			double e = Double.parseDouble(log.getEnergy());
			sum += e;
		}
		return String.valueOf(Math.round(sum));
	}
	
	public Map<String,List<FoodLog>> getLog(String date){
		Map<String,List<FoodLog>> map = new HashMap<String,List<FoodLog>>();
		List<FoodLog> breakfast = new ArrayList<FoodLog>();
		List<FoodLog> lunch = new ArrayList<FoodLog>();
		List<FoodLog> dinner = new ArrayList<FoodLog>();
		List<FoodLog> snack = new ArrayList<FoodLog>();
		
		List<FoodLog> logs = dao.getFoodLog(date);
		for(FoodLog log : logs){
			if(PlanItem.PERIOD_BREAKFAST.equalsIgnoreCase(log.getPeriod())){
				breakfast.add(log);
			}else if(PlanItem.PERIOD_LUNCH.equalsIgnoreCase(log.getPeriod())){
				lunch.add(log);
			}else if(PlanItem.PERIOD_DINNER.equalsIgnoreCase(log.getPeriod())){
				dinner.add(log);
			}else if(PlanItem.PERIOD_SNACK.equalsIgnoreCase(log.getPeriod())){
				snack.add(log);
			}
		}
		
		map.put(PlanItem.PERIOD_BREAKFAST, breakfast);
		map.put(PlanItem.PERIOD_LUNCH, lunch);
		map.put(PlanItem.PERIOD_DINNER, dinner);
		map.put(PlanItem.PERIOD_SNACK, snack);
		
		return map;
	}
	
	public Map<String,List<FoodLog>> getTodayLog(){
		return getLog(sf.format(Calendar.getInstance().getTime()));
	}
	
	public List<DailyEnergy> listAllDailyEnergy(){
		return dao.listDailyEnergy();
	}
	
	public List<DailyEnergy> getEnergyLog(){
		Calendar c = Calendar.getInstance();
		List<DailyEnergy> lst = dao.getEnergyByDate(c);		
		List<DailyEnergy> del = new ArrayList<DailyEnergy>();
		c = Calendar.getInstance();
		if(lst!=null && lst.size()<7){
			for(int i=0; i<7; i++){
				String date = sf.format(c.getTime());
				c.add(Calendar.DAY_OF_MONTH, -1);
				boolean f = false;
				
				for(DailyEnergy de : lst){
					if(de.getDate().equals(date)){
						del.add(de);
						f = true;
						break;
					}
				}
				
				if(!f){
					del.add(new DailyEnergy(date,"-"));
				}
			}
		}else{
			del = lst;
		}		
		
		return del;
	}
	
	public boolean deleteFoodLog(String id){
		return dao.deleteFoodLog(id);
	}
	
	public boolean addFoodLog(FoodLog log){
		return dao.addFoodLog(log);
	}
	
}
